package com.sxc.loglib;

import android.content.Context;
import android.os.Environment;

import java.io.File;

/**
 * Create by sunxunchao on 2019/9/17
 */
public class LogConfig {

    private boolean writeLog = false;

    private String fileName = "log.log";//log日志的文件名称
    private String folderName = "log";
    private String crashName = "crash.log";

    private String logFilePath;
    private boolean isSaveExternal = false;
    private boolean useShake = true;

    private long saveFileTime = 1 * 24 * 60 * 60 * 1000; //一天
    private long fileSize = 100000;//日志文件的大小，默认0.1M


    private static class INSTANCE {

        private static LogConfig logConfig = new LogConfig();
    }

    public static LogConfig getInstance() {
        return INSTANCE.logConfig;
    }

    private LogConfig() {
    }

    public LogConfig isWriteLog(boolean isWriteLog) {
        writeLog = isWriteLog;
        return this;
    }

    public boolean getIsWriteLog() {
        return writeLog;
    }

    public LogConfig fileSize(long size) {
        this.fileSize = size;
        return this;
    }

    public long getFileSize() {
        return this.fileSize;
    }

    public String getLogFilePath() {
        return logFilePath;
    }

    public String getFileName() {
        return fileName;
    }

    public LogConfig setFileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    public String getFolderName() {
        return folderName;
    }

    public LogConfig setFolderName(String folderName) {
        this.folderName = folderName;
        return this;
    }

    public boolean getSaveExternal() {
        return isSaveExternal;
    }

    public LogConfig isSaveExternal(boolean saveExternal) {
        isSaveExternal = saveExternal;
        return this;
    }

    public String getCrashName() {
        return crashName;
    }

    public LogConfig setCrashName(String crashName) {
        this.crashName = crashName;
        return this;
    }

    public long getSaveFileTime() {
        return saveFileTime;
    }

    public LogConfig setSaveFileTime(long saveFileTime) {
        this.saveFileTime = saveFileTime;
        return this;
    }

    public boolean isUseShake() {
        return useShake;
    }

    public LogConfig setUseShake(boolean useShake) {
        this.useShake = useShake;
        return this;
    }

    public LogConfig build(Context context) {
        if (isSaveExternal) {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中
                logFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + folderName + File.separator;
            } else {
                logFilePath = context.getFilesDir().getAbsolutePath() + File.separator + folderName + File.separator;
            }
        } else {
            logFilePath = context.getFilesDir().getAbsolutePath() + File.separator + folderName + File.separator;
        }
        File file = new File(logFilePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        FileUtils.deleteOverdueFile();

        if (useShake) {
            new SensorHelper(context);
        }

        return this;
    }

    public void start() {
        if (writeLog) {
            new CrashHelper();
            new LogHelper().start();
        }
    }
}
